﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using AutoLoL.Entities;
using AutoLoL.Logic.Repositories;
using AutoLoL.Shared;
using AutoLoL.Logic.Factories;

namespace AutoLoL.Logic.Masteries
{
    public class LolmMasteryImporter
    {
        public static IEnumerable<ChampionBuild> Import(string filePath, IChampionRepository championRepository, IMasteryRepository masteryRepository)
        {
            List<ChampionBuild> builds = new List<ChampionBuild>();
            string buildName = Path.GetFileNameWithoutExtension(filePath);

            string masteryString = null;
            string filter = string.Empty;

            string text;
            using (StreamReader sr = new StreamReader(filePath))
            {
                text = sr.ReadToEnd();
            }

            if (text.Trim().Length == 43)
            {
                masteryString = text;
            }
            else
            {
                XDocument xDoc = XDocument.Parse(text);
                string value = xDoc.Root.Value;
                masteryString = value.Substring(0, 43);

                if (value.Length > 43)
                {
                    filter = value.Substring(43);
                }

            }

            if (string.IsNullOrEmpty(buildName) || string.IsNullOrEmpty(masteryString))
            {
                return null;
            }

            List<Champion> champions = new List<Champion>();
            foreach (Champion champion in championRepository.GetAll())
            {
                string name = champion.Name.ToLower();

                if (buildName.ToLower().Contains(name) || buildName.ToLower().Contains(name.Replace(" ", ""))
                    || filter.ToLower().Contains(name) || filter.ToLower().Contains(name.Replace(" ", "")))
                {
                    champions.Add(champion);
                }
            }

            if (champions.Count == 0)
            {
                champions.Add(championRepository.GetAll()[0]);
            }

            int index = 1;
            foreach (Champion champion in champions)
            {
                MasteryBuild masteryBuild = masteryRepository.GetMasteryBuild();
                MasteryBuildManager manager = new MasteryBuildManager(masteryBuild, MasteryFactory.Instance, SummonerSpellFactory.Instance);
                manager.FromString(masteryString);

                ChampionBuild championBuild = new ChampionBuild()
                {
                    Champion = champion,
                    MasteryBuild = masteryBuild,
                    Title = buildName,
                    SummonerSpells = new[]
                    {
                        masteryBuild.ImprovedSummonerSpells.ElementAtOrDefault(0),
                        masteryBuild.ImprovedSummonerSpells.ElementAtOrDefault(1)
                    }
                };

                if (champions.Count > 1)
                {
                    championBuild.Title += string.Format(" ({0})", index);
                }

                builds.Add(championBuild);

                index++;
            }

            return builds;
        }
    }
}
